#include "Game.hpp"
#include "Utils.hpp"
#include "PhysicEngine.hpp"
#include "InputManager.hpp"

Game::Game (sf::RenderWindow &renderWindow):
m_renderer(renderWindow),
m_tankManager(),
m_map(),
m_maskTask(NO_TASK)
{
	InputManager::createInstance(renderWindow.GetInput());
}
Game:: ~Game ()
{
	m_tankManager.clearTanks();
	m_map.clearBoxes();
	PhysicEngine::instance().destroyWorld();
	InputManager::destroyInstance();
}

void Game::load()
{
	// physic
	PhysicEngine::instance().createWorld();

	// map
	m_map.setBackground("granite");
	m_map.loadModele("cross");
	
	// tank
	m_tankManager.createPlayerTank(0);
}

void Game::update()
{
	if(m_maskTask & INPUT_TASK)
	{
		InputManager::instance().flushEvents();
		InputManager::instance().update();
	}
	
	if(m_maskTask & SIMULATION_TASK)
	{
		PhysicEngine::instance().world()->ClearForces();
		PhysicEngine::instance().world()->Step(1/60.f,5,7);
		m_tankManager.update();

	}
	
	if (m_maskTask & RENDERER_TASK)
	{
		m_renderer.drawBackground(m_map);
		m_renderer.drawTank(m_tankManager.tank(0));
		m_renderer.drawBoxes(m_map);
		// draw bullets
		// draw explosions
	}
}

void Game::setTasks(char maskTask)
{
	m_maskTask = maskTask;
}

